package com.cgnpc.cuddemo.funcInstance.controller;

import cn.com.cgnpc.aep.bizcenter.appcenter.sdk.client.RestClient;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cgnpc.cud.annotation.OperatorType;
import com.cgnpc.cud.annotation.UBA;
import com.cgnpc.cud.boot.autoconfigure.aep.AepProperties;
import com.cgnpc.cud.core.controller.BaseController;
import com.cgnpc.cud.core.domain.AjaxResult;
import com.cgnpc.cuddemo.funcInstance.application.ICudFileApplication;
import com.cgnpc.cuddemo.funcInstance.application.ICudWinApplication;
import com.cgnpc.cuddemo.funcInstance.domain.CudFile;
import com.cgnpc.cuddemo.funcInstance.domain.CudWin;
import com.cgnpc.cuddemo.funcInstance.service.ICudFileService;
import com.cgnpc.cuddemo.funcInstance.service.ICudWinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/******************************
 * 用途说明: 前端控制器
 * 作者姓名: PXMWRYA
 * 创建时间: 2019-06-17 14:39
 ******************************/
@RestController
@RequestMapping("/win/cud-win")
public class CudWinController extends BaseController {

    @Autowired
    ICudWinService cudWinService;

    @Autowired
    ICudFileApplication cudFileApplication;

    @Autowired
    ICudWinApplication cudWinApplication;

    @Autowired
    ICudFileService cudFileService;

    @Autowired
    RestClient restClient;

    @Autowired
    AepProperties aepProperties;

    /**********************************
    * 用途说明: 获取详情
    * 参数说明 id
    * 返回值说明:
    ***********************************/
    @PostMapping("/getWinDetails")
//    @RequiresPermissions("test")
//    @UBA(module = "兑奖模块",action = "获取兑奖信息详情",channel = OperatorType.Button)
    public AjaxResult getWinDetails(String id) {
        try {
//            CudWin win = cudWinService.getById(id);
            CudWin win = new CudWin();
            return getObject(win);
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**********************************
    * 用途说明: 获取得奖列表
    * 参数说明 params
    * 返回值说明:
    ***********************************/
    @PostMapping("getWinList")
//    @UBA(module = "兑奖模块",action = "获取兑奖列表",channel = OperatorType.Menu)
    public AjaxResult getWinList(@RequestBody String params) {
        try {
            IPage page = cudWinApplication.getWinList(params);
            return getObject(page);
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**********************************
    * 用途说明: 添加操作  经过流程
    * 参数说明 params
    * 返回值说明:
    ***********************************/
    @PostMapping("/insertWin")
//    @RequiresPermissions("win_add")
//    @UBA(module = "兑奖模块",action = "新增兑奖走流程",channel = OperatorType.Button)
    public AjaxResult insertWin(@RequestBody String params) {
        try {
            boolean flag = false;
            flag = cudWinApplication.insertWin(params);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**********************************
    * 用途说明: 添加中奖信息  不走流程
    * 参数说明 params
    * 返回值说明:
    ***********************************/
    @PostMapping("/addWin")
//    @UBA(module = "兑奖模块",action = "新增兑奖不走流程",channel = OperatorType.Button)
    public AjaxResult addWin(@RequestBody String params) {
        try {
            //停顿3秒 测试重复提交  :loading 和 :disabled  或者axios的全局拦截中过滤请求做"加载中"的效果
//            Thread.sleep(3000);
            boolean flag = false;
            flag = cudWinApplication.addWin(params);
            if (flag) {
                return success();
            } else {
                return error("添加流程失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**********************************
    * 用途说明: 保存兑奖信息
    * 参数说明 params
    * 返回值说明:
    ***********************************/
    @PostMapping("/saveWin")
//    @UBA(module = "兑奖模块",action = "保存兑奖信息",channel = OperatorType.Button)
    public AjaxResult saveWin(@RequestBody String params) {
        try {
            boolean flag = false;
            flag = cudWinApplication.saveWin(params);
            if (flag) {
                return success();
            } else {
                return error("保存兑奖信息失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**********************************
    * 用途说明: 逻辑删除操作
    * 参数说明 winId
    * 返回值说明:
    ***********************************/
    @PostMapping("/delWin")
//    @UBA(module = "兑奖模块",action = "删除兑奖信息",channel = OperatorType.Button)
    public AjaxResult delWin(String winId) {
        try {
            boolean flag = false;
            flag = cudWinApplication.delWin(winId);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**********************************
    * 用途说明: 通过流程实例的procGuid 来匹配 信息
    * 参数说明 id
    * 返回值说明:
    ***********************************/
    @PostMapping("getWinDetailsByProcInfo")
//    @UBA(module = "流程模块",action = "通过流程实例信息获取兑奖信息",channel = OperatorType.Page)
    public AjaxResult getWinDetailsByProcInfo(String id) {
        try {
            CudWin win = cudWinApplication.getWinDetailsByProcInfo(id);
            return getObject(win);
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**********************************
    * 用途说明: 批量删除
    * 参数说明 params
    * 返回值说明:
    ***********************************/
    @PostMapping("/batchDelWin")
//    @UBA(module = "兑奖模块",action = "批量删除兑奖信息",channel = OperatorType.Button)
    public AjaxResult batchDelWin(@RequestBody String params) {
        try {
            boolean flag = false;
            flag = cudWinApplication.batchDelWin(params);
            if (flag) {
                return success();
            } else {
                return error("批量删除失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**********************************
    * 用途说明: 删除业务数据以及我的草稿
    * 参数说明 paramsMap
    * 返回值说明:
    ***********************************/
    @PostMapping("/deleteWinAndMyDraft")
//    @UBA(module = "流程模块",action = "删除兑奖信息以及我的草稿",channel = OperatorType.Button)
    public AjaxResult deleteWinAndMyDraft(@RequestBody Map paramsMap){
        try{
            boolean result = cudWinApplication.deleteWinAndMyDraft(paramsMap);
            if(result){
                return success();
            }else{
                return error("删除失败!");
            }
        }catch (Exception e){
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**********************************
    * 用途说明: 编辑表单数据以及草稿
    * 参数说明 paramsMap
    * 返回值说明:
    ***********************************/
    @PostMapping("/saveWinAndDraft")
//    @UBA(module = "兑奖模块",action = "保存兑奖信息及草稿表",channel = OperatorType.Button)
    public AjaxResult saveWinAndDraft(@RequestBody Map paramsMap){
        try{
            boolean flag = cudWinApplication.saveWinAndDraft(paramsMap);
            if(flag){
                return success();
            }else{
                return error("修改草稿失败!");
            }
        }catch (Exception e){
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**********************************
    * 用途说明: 提交业务数据以及草稿  存在草稿的情况  业务数据改变状态即可
    * 参数说明 params
    * 返回值说明:
    ***********************************/
    @PostMapping("/commitWinAndDraft")
//    @UBA(module = "流程模块",action = "提交兑奖信息和草稿",channel = OperatorType.Button)
    public AjaxResult commitWinAndDraft(@RequestBody String params){
        try{
            boolean flag = cudWinApplication.commitWinAndDraft(params);
            if(flag){
                return success();
            }else{
                return error("提交失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**********************************
    * 用途说明: 出业务excel数据
    * 参数说明 paramsMap
    * 返回值说明:
    ***********************************/
    @PostMapping("/exportWinExcel")
//    @UBA(module = "兑奖模块",action = "导出excel中奖列表",channel = OperatorType.Button)
    public void exportWinExcel(@RequestBody Map paramsMap){
        try{
            cudWinApplication.exportWinExcel(paramsMap);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**********************************
    * 用途说明: 上传文件 批量增加
    * 参数说明 file
    * 返回值说明:
    ***********************************/
    @PostMapping("/upExcelFile")
//    @UBA(module = "兑奖模块",action = "上传文件",channel = OperatorType.Button)
    public AjaxResult upExcelFile(MultipartFile file) throws Exception{
        try{
            boolean flag = cudWinApplication.upExcelFile(file);
            if(flag){
                return success();
            }else{
                return error("批量导入失败，请检查");
            }
        }catch (Exception e){
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 下载导入模板
     * 参数说明 file
     * 返回值说明:
     ***********************************/
    @PostMapping("/uploadTemp")
//    @UBA(module = "兑奖模块",action = "下载导入模板",channel = OperatorType.Button)
    public void uploadTemp(){
        try{
            cudWinApplication.uploadTemp();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**********************************
     * 用途说明: 上传文件
     * 参数说明 file
     * 返回值说明:
     ***********************************/
    @PostMapping("upfile")
//    @UBA(module = "文件模块",action = "上传文件", channel = OperatorType.Button)
    public AjaxResult upfile(MultipartHttpServletRequest request, MultipartFile file, HttpServletResponse response){
        try{

            response.setHeader("Content-type","textml;charset=UTF-8");
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            //测试中台的文件上传微服务
//            UploadBaseInfoVO uploadBaseInfoVO = new UploadBaseInfoVO();
//            uploadBaseInfoVO.setTenantIdParam("1");
//            uploadBaseInfoVO.setTenantCode("CGNP");
//            uploadBaseInfoVO.setTenantName("中国广核电力股份有限公司");
//            uploadBaseInfoVO.setAppIdParam("");
//            uploadBaseInfoVO.setAppCode("");
//            uploadBaseInfoVO.setAppName("");
//            uploadBaseInfoVO.setUploadUserId("1");
//            uploadBaseInfoVO.setUploadUserCode("PXMWRYA");
//            uploadBaseInfoVO.setUploadUserName("阮洋");

//            String realServiceUrl = Constants.FILECENTER_URL + "/uploadFiles";
//            String url = CgnHeader.getUrl(aepProperties.getActive()) + realServiceUrl;

//            CgnRequestHeader header = null;
//            ApiResult result = null;
//
//            header = CgnHeader.getHeader(realServiceUrl,aepProperties);
//            List<MultipartFile> multipartFiles = request.getFiles("file");
//            if (multipartFiles == null || multipartFiles.size() < 1) {
//                return error("未选择文件,请检查");
//            }
////            调用远程接口
//            result = restClient.postCgnFileForRest(url,uploadBaseInfoVO,header,multipartFiles);
//            result = restClient.postCgnFileForRest(url,fileMap,header,multipartFiles);
//            return getObject(result);

            // ftp 上传文件服务器
            CudFile cudFile = cudFileApplication.upfile(file);
            if(cudFile != null){
                return getObject(cudFile);
            }else{
                return error("保存文件信息失败,请检查");
            }
        }catch (Exception e){
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 删除文件
     * 参数说明 map
     * 返回值说明:
     ***********************************/
    @PostMapping("delFile")
//    @UBA(module = "文件模块",action = "删除文件", channel = OperatorType.Button)
    public AjaxResult delFile(@RequestBody Map map){
        try{
            boolean flag = false;
            flag = cudFileApplication.delFile(map);
            if(flag){
                return success();
            }else{
                return error("删除文件失败!");
            }
        }catch (Exception e){
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 查询文件列表
     * 参数说明 map
     * 返回值说明:
     ***********************************/
    @PostMapping("/findFileList")
//    @UBA(module = "文件模块",action = "查询文件列表", channel = OperatorType.Page)
    public AjaxResult findFileList(@RequestBody Map map){
        try{
            String winId = map.get("winId").toString();
            List<CudFile> fileList = cudFileService.list(new QueryWrapper<CudFile>().eq("file_win_id",winId));
            return getObject(fileList);
        }catch(Exception e){
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 通过流程的guid 来获取 中奖信息 再通过中奖信息获取文件列表
     * 参数说明 map
     * 返回值说明:
     ***********************************/
    @PostMapping("/findFileListByProcGuid")
//    @UBA(module = "流程模块",action = "获取文件列表", channel = OperatorType.Page)
    public AjaxResult findFileListByProcGuid(@RequestBody Map map){
        try{
            String guid = map.get("guid").toString();
            CudWin win = cudWinService.getOne(new QueryWrapper<CudWin>().eq("cud_proc_ins",guid));
            List<CudFile> fileList = null;
            if(win != null){
                fileList = cudFileService.list(new QueryWrapper<CudFile>().eq("file_win_id",win.getCudWinId()));
            }
            return getObject(fileList);
        }catch(Exception e){
            e.printStackTrace();
            return error(e.getMessage());
        }
    }


    /**********************************
     * 用途说明: 下载文件
     * 参数说明 fileMap
     * 返回值说明:
     ***********************************/
    @PostMapping("downloadFile")
//    @UBA(module = "文件模块",action = "下载文件", channel = OperatorType.Page)
    public void downloadFile(@RequestBody Map fileMap){
        try{
            cudFileApplication.downloadFile(fileMap);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}